package com.iwdnb.gkgz.application.utils;

import java.util.ArrayList;
import java.util.List;

public class TradingSignals {

    /**
     * 数据实体类，包含开盘价、收盘价、最高价和最低价。
     */
    public static class PriceData {
        double open;
        double close;
        double high;
        double low;

        public PriceData(double open, double close, double high, double low) {
            this.open = open;
            this.close = close;
            this.high = high;
            this.low = low;
        }
    }

    /**
     * 获取前N周期的连续价格数据。
     */
    private static PriceData getRef(List<PriceData> prices, int N) {
        int index = prices.size() - 1 - N;
        if (index < 0 || index >= prices.size()) {
            throw new IndexOutOfBoundsException("引用周期超出数据范围");
        }
        return prices.get(index);
    }

    /**
     * 获取过去N周期内最低价的最低值。
     */
    private static double getLLV(List<PriceData> prices, int N) {
        return prices.stream()
            .skip(Math.max(0, prices.size() - N))
            .mapToDouble(p -> p.low)
            .min()
            .orElseThrow(() -> new IllegalArgumentException("没有足够的数据计算LLV"));
    }

    /**
     * 获取过去N周期内最高价的最高值。
     */
    private static double getHHV(List<PriceData> prices, int N) {
        return prices.stream()
            .skip(Math.max(0, prices.size() - N))
            .mapToDouble(p -> p.high)
            .max()
            .orElseThrow(() -> new IllegalArgumentException("没有足够的数据计算HHV"));
    }

    public static void main(String[] args) {
        List<PriceData> historicalData = new ArrayList<>(); // 模拟数据填充这里
        final int N = 10; // 使用的周期长度，根据具体公式调整

        // 检查是否有足够的数据
        if (historicalData.size() <= N) {
            System.out.println("没有足够的历史数据");
            return;
        }

        // 当前周期的数据
        PriceData currentData = historicalData.get(historicalData.size() - 1);

        // REF值
        PriceData refDataN = getRef(historicalData, N);
        PriceData refDataNPlusOne = getRef(historicalData, N + 1);

        // LLV和HHV值
        double llv = getLLV(historicalData, N);
        double hhv = getHHV(historicalData, N);

        // S1
        boolean S1 = hhv / llv <= 1.15;

        // S2
        boolean S2 = refDataN.close / refDataNPlusOne.close >= 1.093 &&
            refDataN.high != refDataN.open &&
            refDataN.close == refDataN.high;

        // S3
        boolean S3 = currentData.low > refDataN.low &&
            currentData.high / refDataN.high <= 1.08 &&
            llv >= refDataN.low;

        // S4
        boolean S4 = currentData.close < refDataN.close;

        // S5
        boolean S5 = S1 && S2 && S3 && S4;

        // 输出结果
        System.out.println("S1: " + S1);
        System.out.println("S2: " + S2);
        System.out.println("S3: " + S3);
        System.out.println("S4: " + S4);
        System.out.println("S5 (所有条件均满足): " + S5);
    }
}

